﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Entity;
using LoadNorthwindDB;

namespace Ex7.ConcurrencyProblem
{
    public class CP
    {
        //WTF?
        public static void Main()
        {
            NorthwindEntities ent1 = new NorthwindEntities();
            NorthwindEntities ent2 = new NorthwindEntities();
            using (ent1)
            {
                Customer customer = ent1.Customers.FirstOrDefault(c => c.CustomerID == "99999");
                Console.WriteLine("\nEntOne Before change:");
                Console.WriteLine("Name: {0}",customer.ContactName);
                Console.WriteLine("City: {0}",customer.City);

                string newName =  customer.ContactName + "1";
                Console.WriteLine("\nEntityOne is changing {0} to {1}!", customer.ContactName, newName);
                customer.ContactName = newName;

                using (ent2)
                {
                Console.WriteLine(new string('-',25));
                    Customer customer2 = ent2.Customers.FirstOrDefault(c => c.CustomerID == "99999");
                    string newCity = customer.City + "2";

                    Console.WriteLine("EntTwo before change:");
                    Console.WriteLine("Name: {0}", customer2.ContactName);
                    Console.WriteLine("City: {0}", customer2.City);
                    Console.WriteLine("\n");

                    Console.WriteLine("EntityTwo is changing {0} to {1}!", customer2.City, newCity);
                    customer2.City = newCity;

                    Console.WriteLine("\nEntTwo after change:");
                    Console.WriteLine("Name: {0}", customer2.ContactName);
                    Console.WriteLine("City: {0}", customer2.City);
                    Console.WriteLine("\n");

                    Console.WriteLine("EntityTwo is saving Changes!");
                    ent2.SaveChanges();

                    customer2 = ent2.Customers.FirstOrDefault(c => c.CustomerID == "99999");
                    CheckObjectInBase();

                    Console.WriteLine(new string('-', 25));
                }
                Console.WriteLine("Before entOne save changes:");
                Console.WriteLine("Name: {0}", customer.ContactName);
                Console.WriteLine("City: {0}", customer.City);
                Console.WriteLine("\n");

                Console.WriteLine("EntityOne is saving Changes!");
                ent1.SaveChanges();

                customer = ent1.Customers.FirstOrDefault(c => c.CustomerID == "99999");
                CheckObjectInBase();

            }
        }

        private static void CheckObjectInBase()
        {
            NorthwindEntities newEntity = new NorthwindEntities();
            using (newEntity)
            {
                Console.WriteLine("\nObject In Base:");
                Customer newCustomer = newEntity.Customers.FirstOrDefault(c => c.CustomerID == "99999");
                Console.WriteLine("\nAfter change:");
                Console.WriteLine("Name: {0}", newCustomer.ContactName);
                Console.WriteLine("City: {0}", newCustomer.City);
            }
        }
    }
}
